package sjtu.ist.warehouse4ws.action;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import sjtu.ist.warehouse4ws.model.CustDomainmodel;
import sjtu.ist.warehouse4ws.model.CustDomainmodelattr;
import sjtu.ist.warehouse4ws.model.CustDomainmodelstate;
import sjtu.ist.warehouse4ws.model.CustEpcunit;
import sjtu.ist.warehouse4ws.model.CustEpcunits;
import sjtu.ist.warehouse4ws.model.CustFunctionpoint;
import sjtu.ist.warehouse4ws.model.CustFunctions;
import sjtu.ist.warehouse4ws.model.CustParameter;
import sjtu.ist.warehouse4ws.model.CustPermission;
import sjtu.ist.warehouse4ws.model.CustProcess;
import sjtu.ist.warehouse4ws.model.CustRole;
import sjtu.ist.warehouse4ws.model.Domainmodel;
import sjtu.ist.warehouse4ws.model.Domainmodelattr;
import sjtu.ist.warehouse4ws.model.Domainmodelstate;
import sjtu.ist.warehouse4ws.model.Epcunit;
import sjtu.ist.warehouse4ws.model.Epcunits;
import sjtu.ist.warehouse4ws.model.Functionpoint;
import sjtu.ist.warehouse4ws.model.Functions;
import sjtu.ist.warehouse4ws.model.Parameter;
import sjtu.ist.warehouse4ws.model.Permission;
import sjtu.ist.warehouse4ws.model.Process;
import sjtu.ist.warehouse4ws.model.Role;
import sjtu.ist.warehouse4ws.service.DomainModelAttributeService;
import sjtu.ist.warehouse4ws.service.DomainModelService;
import sjtu.ist.warehouse4ws.service.DomainModelStateService;
import sjtu.ist.warehouse4ws.service.EpcUnitService;
import sjtu.ist.warehouse4ws.service.EpcunitsService;
import sjtu.ist.warehouse4ws.service.FunctionpointService;
import sjtu.ist.warehouse4ws.service.FunctionsService;
import sjtu.ist.warehouse4ws.service.ParameterService;
import sjtu.ist.warehouse4ws.service.PermissionService;
import sjtu.ist.warehouse4ws.service.ProcessService;
import sjtu.ist.warehouse4ws.service.RoleService;
import sjtu.ist.warehouse4ws.service.cust.CustDomainModelAttributeService;
import sjtu.ist.warehouse4ws.service.cust.CustDomainModelService;
import sjtu.ist.warehouse4ws.service.cust.CustDomainModelStateService;
import sjtu.ist.warehouse4ws.service.cust.CustEpcUnitService;
import sjtu.ist.warehouse4ws.service.cust.CustEpcunitsService;
import sjtu.ist.warehouse4ws.service.cust.CustFunctionpointService;
import sjtu.ist.warehouse4ws.service.cust.CustFunctionsService;
import sjtu.ist.warehouse4ws.service.cust.CustParameterService;
import sjtu.ist.warehouse4ws.service.cust.CustPermissionService;
import sjtu.ist.warehouse4ws.service.cust.CustProcessService;
import sjtu.ist.warehouse4ws.service.cust.CustRoleService;
import sjtu.ist.warehouse4ws.tool.Constants;

import com.opensymphony.xwork2.ActionSupport;

public class CopyTableAction extends ActionSupport{

	private DomainModelAttributeService domainModelAttributeService;
	private DomainModelService domainModelService;
	private DomainModelStateService domainModelStateService;
	private FunctionpointService functionpointService;
	private FunctionsService functionsService;
	private ProcessService processService;
	private ParameterService parameterService;
	private PermissionService permissionService;
	private RoleService roleService;
	
	private CustDomainModelAttributeService custDomainModelAttributeService;
	private CustDomainModelService custDomainModelService;
	private CustDomainModelStateService custDomainModelStateService;
	private CustFunctionpointService custFunctionPointService;
	private CustProcessService custProcessService;
	private CustParameterService custParameterService;
	private CustPermissionService custPermissionService;
	private CustRoleService custRoleService;
	private CustFunctionsService custFunctionsService;
	

	private EpcunitsService epcunitsService;
	private EpcUnitService epcUnitService;
	
	private CustEpcunitsService custEpcunitsService;
	private CustEpcUnitService custEpcUnitService;
	
	public String copyGroupModel(Integer groupId,String companyId){
		List<Domainmodel> list1=new ArrayList();
		List<Domainmodelattr> list2=new ArrayList();
		List<Domainmodelstate> list3=new ArrayList();
		list1=domainModelService.getGroupDomainModel(groupId);
		for(int i=0;i<list1.size();i++){
			String domainid=list1.get(i).getId();
			if(domainid!=null){

				List AttributeList=domainModelAttributeService.getDomainModelAttributeList(domainid);
				List stateList=domainModelStateService.getDomainModelStateList(domainid);
			if(AttributeList!=null){
				for(int j=0;j<AttributeList.size();j++){
					list2.add((Domainmodelattr) AttributeList.get(j));
				}
		//	list2.addAll((Collection<? extends Domainmodelattr>)AttributeList);
			}
			if(stateList!=null){
				for(int k=0;k<stateList.size();k++){
					list3.add((Domainmodelstate) stateList.get(k));
				}
		//	list3.addAll((Collection<? extends Domainmodelstate>)stateList);
			}
			}	
		}
	CustDomainmodel dm=new CustDomainmodel();	
	if(list1!=null){
	for(int i=0;i<list1.size();i++){
		dm.setCategory(list1.get(i).getCategory());
		dm.setCnName(list1.get(i).getCnName());
		dm.setDescription(list1.get(i).getDescription());
		dm.setEnName(list1.get(i).getDescription());
		dm.setFileId(null);
		dm.setId(list1.get(i).getFileId());  //表名新建
		dm.setType(list1.get(i).getType());
		custDomainModelService.saveCustDomainModel(dm);
	}}
	
	CustDomainmodelattr dm2=new CustDomainmodelattr();
	if(list2!=null){
	for(int i=0;i<list2.size();i++){
	dm2.setCnName(list2.get(i).getCnName());
	dm2.setCnType(list2.get(i).getCnType());
	dm2.setCollectionType(list2.get(i).getCollectionType());
	dm2.setDefaultValue(list2.get(i).getDefaultValue());
	dm2.setDescription(list2.get(i).getDescription());
	dm2.setDomainModelid(list2.get(i).getDomainModelid());
	dm2.setEnName(list2.get(i).getEnName());
	dm2.setEnType(list2.get(i).getEnType());
	dm2.setId(list2.get(i).getId());
	dm2.setMaxVal(list2.get(i).getMaxVal());
	dm2.setMinVal(list2.get(i).getMinVal());
	dm2.setReadOnly(list2.get(i).getReadOnly());
	dm2.setStaticstate(list2.get(i).getStaticstate());
	dm2.setTypeId(list2.get(i).getTypeId());
	dm2.setUni(list2.get(i).getUni());
	custDomainModelAttributeService.SaveCustDomainModelAttribute(dm2);
	}}
	CustDomainmodelstate dm3=new CustDomainmodelstate();
	if(list3!=null){
	for(int i=0;i<list3.size();i++){

	dm3.setCnName(list3.get(i).getCnName());
	dm3.setDescription(list3.get(i).getDescription());
	dm3.setDomainModelid(list3.get(i).getDomainModelid());
	dm3.setEnName(list3.get(i).getEnName());
	dm3.setId(list3.get(i).getId());
	custDomainModelStateService.savaCustDomainmodelstate(dm3);
	}
	}
	return SUCCESS;
	
	}
	
public String copyGroupEpc(Integer groupId,String companyId){
		
		List<Epcunits> epcunitsList=new ArrayList<Epcunits>();
		List<Epcunit> epcunitList=new ArrayList<Epcunit>();
		
		epcunitsList=epcunitsService.getEpcunitsListByGroupId(groupId,Constants.ADMIN_COMPANY_ID);
		for(int i=0;i<epcunitsList.size();i++){
			String epcunitsId=epcunitsList.get(i).getId();
			if(epcunitsId!=null){
				List<Epcunit> tempList=epcUnitService.getEpcunitListById(epcunitsId);
				if (tempList!=null)
			          epcunitList.addAll(tempList);
			}	
		}
		
		CustEpcunits custEpcunits=new CustEpcunits();	
		if(epcunitsList!=null){
		for(int i=0;i<epcunitsList.size();i++){
			custEpcunits=(CustEpcunits)epcunitsList.get(i);
			custEpcunitsService.saveCustEpcUnits(custEpcunits);
		                                     }
		}
		List<CustEpcunit> custEpcunitList=new ArrayList<CustEpcunit>();
		if (epcunitList!=null){
			for (int i=0;i<epcunitList.size();i++){
				CustEpcunit custEpcunit=new CustEpcunit(epcunitList.get(i));
				custEpcunitList.add(custEpcunit);
				
			}
		}
		custEpcUnitService.addCustEpcUnit(custEpcunitList);

		System.out.print(epcunitList.size());
		System.out.print(epcunitsList.size());
		return SUCCESS;
	}
	
	public String copyGroupFunction(Integer groupId,String companyId)
	{
		List<Functionpoint> functionList=
			functionpointService.getFunctionpointlistbyGroupId(groupId);
		List<Functions> functionsList=new ArrayList<Functions>();
		List<Process> processList=new ArrayList<Process>();
		List<Parameter> parameterList=new ArrayList<Parameter>();
		CustFunctionpoint cfp=new CustFunctionpoint();
		if(functionList != null)
			for(int i=0;i<functionList.size();i++)
			{
				cfp=(CustFunctionpoint)functionList.get(i);
				custFunctionPointService.saveCustFunctionpoint(cfp);
				functionsList.addAll((Collection<? extends Functions>)functionsService.getFunctionsbyFunctionId(cfp.getId()));
				processList.addAll((Collection<? extends Process>) functionsService.getProcessbyFunctionId(cfp.getId()));
				parameterList.addAll((Collection<? extends Parameter>) parameterService.getParameterList(cfp.getId()));
			}
		
		if(functionsList != null)
			for(int i=0;i<functionsList.size();i++)
			{
				CustFunctions custFunctions=(CustFunctions)functionsList.get(i);
				custFunctionsService.sava(custFunctions);
			}
		
		if(processList != null)
			for(int i=0;i<processList.size();i++)
			{
				CustProcess custProcess=(CustProcess)processList.get(i);
				custProcessService.save(custProcess);
			}
		
		if(parameterList != null)
			for(int i=0;i<parameterList.size();i++)
			{
				CustParameter custParameter=new CustParameter();
				custParameter=(CustParameter)(parameterList.get(i));
				custParameterService.save(custParameter);
			}
		return SUCCESS;
	}
	
	public String copyGroupPermission(String groupId,String companyId)
	{
		List<Permission> permissionList=permissionService.getPermissionByGroupId(groupId);
		if(permissionList != null)
			for(int i=0;i<permissionList.size();i++)
			{
				CustPermission custPermission=(CustPermission)(permissionList.get(i));
				custPermissionService.addCustPermission(custPermission);
			}
		List<Role> roleList=roleService.getRoleListbyGroupId(groupId,Constants.ADMIN_COMPANY_ID);
		if(roleList != null)
			for(int i=0;i<roleList.size();i++)
			{
				CustRole custRole=(CustRole)(roleList.get(i));
				custRoleService.addCustRole(custRole);
			}
		return SUCCESS;
	}
	
	public String test(){
		Integer st1=1;
		String st2="6";
		copyGroupEpc(st1,st2);
		copyGroupModel(st1,st2);

		return SUCCESS;
	}
	
	public String copyAll(Integer groupId,String companyId){
		
		copyGroupModel(groupId,companyId);
		copyGroupEpc(groupId,companyId);
		copyGroupFunction(groupId,companyId);
		return SUCCESS;
		
	}

	public void setDomainModelAttributeService(
			DomainModelAttributeService domainModelAttributeService) {
		this.domainModelAttributeService = domainModelAttributeService;
	}

	public void setDomainModelService(DomainModelService domainModelService) {
		this.domainModelService = domainModelService;
	}

	public void setDomainModelStateService(
			DomainModelStateService domainModelStateService) {
		this.domainModelStateService = domainModelStateService;
	}

	public void setCustDomainModelAttributeService(
			CustDomainModelAttributeService custDomainModelAttributeService) {
		this.custDomainModelAttributeService = custDomainModelAttributeService;
	}

	public void setCustDomainModelService(
			CustDomainModelService custDomainModelService) {
		this.custDomainModelService = custDomainModelService;
	}

	public void setCustDomainModelStateService(
			CustDomainModelStateService custDomainModelStateService) {
		this.custDomainModelStateService = custDomainModelStateService;
	}

	public void setFunctionpointService(FunctionpointService functionPointService) {
		this.functionpointService = functionPointService;
	}

	public void setFunctionsService(FunctionsService functionsService) {
		this.functionsService = functionsService;
	}

	public void setProcessService(ProcessService processService) {
		this.processService = processService;
	}

	public void setParameterService(ParameterService parameterService) {
		this.parameterService = parameterService;
	}

	public void setPermissionService(PermissionService permissionService) {
		this.permissionService = permissionService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public void setCustFunctionpointService(
			CustFunctionpointService custFunctionPointService) {
		this.custFunctionPointService = custFunctionPointService;
	}
	
	public void setCustFunctionsService(CustFunctionsService custFunctionsService) {
		this.custFunctionsService = custFunctionsService;
	}

	public void setCustProcessService(CustProcessService custProcessService) {
		this.custProcessService = custProcessService;
	}

	public void setCustParameterService(CustParameterService custParameterService) {
		this.custParameterService = custParameterService;
	}

	public void setCustPermissionService(CustPermissionService custPermissionService) {
		this.custPermissionService = custPermissionService;
	}

	public void setCustRoleService(CustRoleService custRoleService) {
		this.custRoleService = custRoleService;
	}

	public void setEpcunitsService(EpcunitsService epcunitsService) {
		this.epcunitsService = epcunitsService;
	}

	public void setEpcUnitService(EpcUnitService epcUnitService) {
		this.epcUnitService = epcUnitService;
	}

	public void setCustEpcunitsService(CustEpcunitsService custEpcunitsService) {
		this.custEpcunitsService = custEpcunitsService;
	}

	public void setCustEpcUnitService(CustEpcUnitService custEpcUnitService) {
		this.custEpcUnitService = custEpcUnitService;
	}
	
	
}
